/* Exercise 9.3 Morse Characters */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly #include "lcdlib.h" #include "utils.h" #define DOT_EVENT '.' // Values for the events #define DASH_EVENT '-' #define SPACE_EVENT 32 #define STUCK_EVENT '*' #define BUSY_STATE 'B' // Values for the states #define QUIET_STATE 'Q' #define DOT_LENGTH 50 // Times in 200ths of a second for dots, dashes and spaces #define DASH_LENGTH 100 #define SPACE_LENGTH 150 #define STUCK_LENGTH 200 #define KEY_BIT 0x01 // Using PORTA bit 0 //#define DEBUG const unsigned char patterns [26] = { // A B C D E F G 2, 7, 5, 3, 1, 13, 1, // H I J K L M N 15, 3, 8, 2, 11, 0, 1, // O P Q R S T U 0, 9, 2, 5, 7, 0, 6, // V W X Y Z 14, 4, 6, 4, 3 }; const unsigned char lengths [26] = { // A B C D E F G 2, 4, 4, 3, 1, 4, 3, // H I J K L M N 4, 2, 3, 3, 4, 2, 2, // O P Q R S T U 3, 4, 4, 3, 3, 1, 3, // V W X Y Z 4, 3, 4, 4, 4 }; unsigned char pattern = 0 ; //Bit pattern of the character being loaded unsigned char length = 0 ; //Length of the character being loaded unsigned char morse_state = QUIET_STATE ; //State variable - set at QUIET unsigned char counter = 0; int time_count = 0 ; unsigned char newporta ; //New version of PORTA unsigned char oldporta ; //Old version of PORTA unsigned char portainputs ; //Local copy of porta void setup_hardware (void) { ANSELA = 0x00; //Set PortA to use digital inputs ANSELB = 0x00; TRISA = 0xFF; //PORTA set to inputs TRISB = 0x00; //PORTB set to all outputs INTCON = 0b10100000; //Configure the INTCON register to enable timer interrupts OPTION_REG = 0b01000100; //Configure the timer to use the prescaler } void tmrHandler( void ) { time_count = time_count + 1 ; //increment our timer newporta = PORTA; //now do the debounce if ( newporta == oldporta ) { portainputs = newporta ; } oldporta = newporta ; //store the old value for next time } //This is the interrupt handler, it is called when the PIC detects an interrupt. //It checks the status bits to find out who caused the interrupt and then calls that handler void interrupt isr( void ) { if( INTCON & 4 ) //if the timer has overflowed bit 2 of INTCON is set high { INTCON = INTCON & 0b11111011; //clear the bit to turn off this interrupt counter = counter + 1; if ( counter > 2 ) { counter = 0; tmrHandler(); //Call the handler function } } } //Given the length and pattern of a character this function will return the matching //character - or '*' if it is not recognised unsigned char decode_character( ) { unsigned char i ; //Search the arrays for a matching pair of length and pattern for ( i=0 ; i < 26 ; i = i + 1 ) { if (pattern == patterns[i]) { if (length == lengths[i]) { //Stop the loop if a match is found break ; } } } if ( i == 26 ) { //If i reached 26 this means that no match was found return '*' ; } else { //Add i onto the code of A to get the character return 'A' + i ; } } //Start is called to reset the pattern and length items void start ( void ) { pattern = 0; length = 0; } //Store dot stores a dot - the existing pattern is shifted left and a dot bit is added then the length is increased void store_dot ( void ) { pattern = pattern << 1 ; pattern = pattern | 1 ; length = length + 1 ; } //Store dash doesn't add a bit but does everything else that store dot does void store_dash ( void ) { pattern = pattern << 1 ; length = length + 1 ; } //We call the function to deal with an event. How it behaves depends on the current state void do_morse ( unsigned char event ) { unsigned char ch ; #ifdef DEBUG lcd_print_ch ( event ) ; lcd_print_ch ( morse_state ) ; #endif switch ( morse_state ) //Use a switch to select the code for the current state { case BUSY_STATE : switch ( event ) //Now have a switch for each event which might happen in this busy state { case DOT_EVENT : //Store the dot here store_dot (); break ; case DASH_EVENT : //Store the dash here store_dash (); break ; case SPACE_EVENT : //Work out the code and move to QUIET state ch = decode_character(); lcd_print_ch ( ch ) ; morse_state = QUIET_STATE ; break ; case STUCK_EVENT : //Go to quiet state morse_state = QUIET_STATE ; } break; case QUIET_STATE : //Now handle events in quiet state switch ( event ) { case DOT_EVENT : //Store the dot here move to BUSY state start () ; store_dot () ; morse_state = BUSY_STATE ; break ; case DASH_EVENT : //Store the dash here move to BUSY state start () ; store_dash () ; morse_state = BUSY_STATE ; break ; case SPACE_EVENT : //Do nothing break ; case STUCK_EVENT : //Do nothing break ; } break; } #ifdef DEBUG lcd_print_ch (morse_state) ; #endif } unsigned char get_morse_key ( void ) { time_count = 0 ; //set the clock at 0 wait for a key down time_count is updated by the interrupt handler while ( time_count < SPACE_LENGTH ) { if ( portainputs & KEY_BIT ) { break ; //quit the loop when the key is pressed } } if ( time_count >= SPACE_LENGTH ) //if the time count has reached SPACE_LENGTH we have a space { return SPACE_EVENT; } time_count = 0 ; //must now time the length of of the key down period while ( time_count < STUCK_LENGTH ) { if ( ( portainputs & KEY_BIT ) == 0 ) { break ; //quit the loop when the key is released } } if ( time_count >= STUCK_LENGTH ) //if time_count has reached stuck length return the stuck down message { return STUCK_EVENT ; } if ( time_count < DOT_LENGTH ) //if the time_count is less than dot - return dot { return DOT_EVENT ; } return DASH_EVENT ; //if we get here we must have a dash } int main (void) { unsigned char i ; setup_hardware(); lcd_start(); while (1) { i = get_morse_key(); do_morse(i); } return 0; }